package com.huage.courseplatform.application.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.huage.courseplatform.application.model.entity.PeriodStudentLeave;
import com.huage.courseplatform.application.model.entity.Student;
import com.huage.courseplatform.application.model.param.StudentLeavePARAM;
import com.huage.courseplatform.application.service.SignService;
import com.huage.courseplatform.framework.annotation.RepeatSubmit;
import com.huage.courseplatform.framework.utils.BeanCopyUtils;
import com.huage.courseplatform.framework.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/sign")
@Api(tags = "对课相关接口")
@Slf4j
public class SignController {

    @Resource
    SignService signService;

    @RepeatSubmit
    @SaCheckRole({"ROLE_TEACHER"})
    @ApiOperation(value = "教师一键签到(自动排除请假学生，二次签到直接覆盖)")
    @PostMapping(value = "/teacherSign")
    public ResultUtils<String> teacherSign(String teacherId,String periodId) {
        Assert.isTrue(signService.teacherHavePeriod(periodId,teacherId),"签到失败,不是负责该课时的老师");
        signService.teacherSign(teacherId,periodId);
        return ResultUtils.success("签到成功",teacherId);
    }

    @RepeatSubmit
    @ApiOperation(value = "学生请假")
    @PostMapping(value = "/studentLeave")
    public ResultUtils<String> studentLeave(@RequestBody @Validated StudentLeavePARAM studentLeavePARAM) {
        PeriodStudentLeave periodStudentLeave = BeanCopyUtils.copyProperties(studentLeavePARAM,PeriodStudentLeave.class);
        signService.studentLeave(periodStudentLeave);
        return ResultUtils.success("请假成功",periodStudentLeave.getStudentId());
    }

    @ApiOperation(value = "获取课时学生请假列表")
    @SaCheckRole({"ROLE_TEACHER"})
    @GetMapping(value = "/getStudentLeave/{periodId}")
    public ResultUtils<List<Student>> getStudentLeave(@PathVariable String periodId) {
        return ResultUtils.success(signService.getStudentLeave(periodId));
    }

    @ApiOperation(value = "获取课时上课学生列表(非请假)")
    @SaCheckRole({"ROLE_TEACHER"})
    @GetMapping(value = "/getStudentSign/{periodId}")
    public ResultUtils<List<Student>> getStudentSign(@PathVariable String periodId) {
        return ResultUtils.success(signService.getStudentSign(periodId));
    }
}
